package com.aduerr.v1shouyin.mvp.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import com.aduerr.v1shouyin.util.ToastUtils;

import java.lang.reflect.Field;

/**
 * Created by fyf on 2018/6/6 0006.
 */

public class KouBeiResultActivity extends AppCompatActivity {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.iv_paysuccess)
    ImageView ivPaysuccess;
    @BindView(R.id.tv_member_name)
    TextView tvMemberName;
    @BindView(R.id.tv_hexiao_time)
    TextView tvHexiaoTime;
    @BindView(R.id.tv_confim)
    TextView tvConfim;

    private String content[] = new String[6];
    private SweetAlertDialog pDialog;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch(msg.what) {
                //显示进度条
                case 1:
                    if(pDialog != null) {
                        pDialog.show();
                    }
                    break;
                case 2:
                    if(pDialog != null) {
                        pDialog.dismiss();
                    }
                    finish();
                    break;
                case 4:
                    if(pDialog != null) {
                        pDialog.dismiss();
                    }
                    toShowDialog();
                    break;
            }
            return false;
        }
    });


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_koubeiresult);
        ButterKnife.bind(this);
        initData();
//        initprint();
    }

    private void initData() {
        tvTitle.setText("核销结果");
        Intent intent = getIntent();
        tvMemberName.setText(intent.getStringExtra("strTicketID"));
        tvHexiaoTime.setText(intent.getStringExtra("UseDate"));
        content[0] = intent.getStringExtra("strTicketID");
        content[1] = intent.getStringExtra("SiteUserName");
        content[2] = intent.getStringExtra("CurrentPrice");
        content[3] = intent.getStringExtra("ItemName");
        content[4] = intent.getStringExtra("OriginalPrice");
        content[5] = intent.getStringExtra("UseDate");
    }

    private void toShowDialog() {
        AlertDialog dialogtits = new AlertDialog.Builder(this,
                R.style.Theme_AppCompat_DayNight_Dialog_Alert)
                .setTitle("提示")
                .setMessage("缺纸,请重新装入打印纸")
                .setCancelable(false)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .show();
        try {
            Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
            mAlert.setAccessible(true);
            Object mAlertController = mAlert.get(dialogtits);
            Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView");
            mMessage.setAccessible(true);
            TextView mMessageView = (TextView) mMessage.get(mAlertController);
            mMessageView.setTextColor(Color.BLACK);
            mMessageView.setTextSize(20);
        } catch(IllegalAccessException e) {
            e.printStackTrace();
        } catch(NoSuchFieldException e) {
            e.printStackTrace();
        }
    }


    @OnClick({R.id.img_back, R.id.tv_confim})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_confim:
                int printpage =
                        (int) SharedPreferencesUtils.getParam(KouBeiResultActivity.this
                                , "printpage", 1);
                if(printpage == 0) {
                    ToastUtils.showToast(KouBeiResultActivity.this, "打印张数设置为不打印," +
                            "请先设置打印张数!!");
                    return;
                }
//                pDialog.show();
                handler.sendEmptyMessage(1);
                if(content != null) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Bitmap bitmap = BitmapFactory.decodeResource(getResources()
                                    , R.drawable.print_public);
                            if(bitmap != null) {
//                                xingPosPrintUtil.toPrintKouBei(content, printpage);
                            }
                        }
                    }).start();
                }
//                finish();
                break;
        }
    }
}
